package com.socialbank.web.util;

import java.io.IOException;

import sun.misc.BASE64Decoder;

@SuppressWarnings("restriction")
public class Base64Util {

	/**
	 * 获取文件的类型
	 * 
	 * @param imgstr
	 * @return
	 */
	public static String getImgType(String imgstr) {
		//System.out.println(imgstr.indexOf("/"));
		//System.out.println(imgstr.indexOf(";"));
		return imgstr.substring(imgstr.indexOf("/") + 1, imgstr.indexOf(";"));
	}

	/**
	 * 图片加密的字符串
	 * @param ingstr
	 * @return
	 */
	public static String getBase64Content(String imgstr) {
		//System.out.println(imgstr.indexOf(","));
		//System.out.println(imgstr.substring(imgstr.indexOf(",") + 1));
		return imgstr.substring(imgstr.indexOf(",") + 1);
	}
	
	 //base64字符串转化成图片  
	public static byte[] GenerateImage(String imgStr){
       //对字节数组字符串进行Base64解码并生成图片  
        if (imgStr == null){
        	//图像数据为空
        	return null;  
        }   
		BASE64Decoder decoder = new BASE64Decoder();  
        //Base64解码  
        byte[] b = null;
		try {
			b = decoder.decodeBuffer(imgStr);
		} catch (IOException e) {
			e.printStackTrace();
		}  
		/*for(int i = 0;i < b.length;++ i)  
        {  
            if(b[i]<0)  
                b[i]+=256;  //调整异常数据  
        }  */
         return b;
    }  
	public static void main(String[] args) {
		//String imgStr = "";
		//System.out.println(getBase64Content(imgStr));
	}
}
